package com.dykj.fireFighting.web.base.emergency;

import com.dykj.fireFighting.api.base.emergency.dto.EmergencyStaffDto;
import com.dykj.fireFighting.api.base.emergency.entity.EmergencyStaff;
import com.dykj.fireFighting.api.base.emergency.service.IEmergencyStaffService;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.system.service.ISysDirectoryService;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.StringJoiner;

@Slf4j
@Api(value = "报警维护人员", tags = {"报警维护人员"})
@RestController
@RequestMapping("/emergencystaff")
public class EmergencyStaffController {
    @DubboReference
    private IEmergencyStaffService iEmergencyStaffService;

    @DubboReference
    private IEnterpriseService enterpriseService;

    @DubboReference
    private ISysDirectoryService sysDirectoryService;

    @ApiOperation(value = "新增报警维护人员信息", notes = "新增报警维护人员信息")
    @PostMapping("/save")
    public CommonResult save(@RequestBody EmergencyStaff emergencyStaff) {
        //前端不知道怎样获取value值，后续这里可以优化
        emergencyStaff.setUnitname(enterpriseService.queryById(emergencyStaff.getUnitid()).getEnterpriseName());
        String str = emergencyStaff.getSystemtype();
        String[] strarr = str.split(",");
        StringJoiner subStr = new StringJoiner(",");
        for (String s : strarr) {
            subStr.add(sysDirectoryService.queryByCode(s).getValue());
        }
        emergencyStaff.setSystemtypename(subStr.toString());
        //emergencyStaff.setSystemtypename(sysDirectoryService.queryByCode(emergencyStaff.getSystemtype()).getValue());
        emergencyStaff.setCenterid(ShiroUtil.getUser().getEnterpriseId().toString());
        emergencyStaff.setIsdel(0);
        iEmergencyStaffService.save(emergencyStaff);
        return CommonResult.success("添加成功");
    }

    @ApiOperation(value = "报警维护人员信息分页", notes = "报警维护人员分页")
    @GetMapping("/list")
    public CommonResult<PageVo<EmergencyStaff>> getpage(EmergencyStaffDto emergencyStaffDto){
        emergencyStaffDto.setCenterid(ShiroUtil.getUser().getEnterpriseId().toString());
        PageVo<EmergencyStaff> pages = iEmergencyStaffService.queryList(emergencyStaffDto);
        System.out.println(pages.toString());
        return CommonResult.success(pages);
    }
    @ApiOperation(value = "修改报警维护人员信息", notes = "修改报警维护人员信息")
    @PostMapping("/update")
    public CommonResult update(@RequestBody EmergencyStaff emergencyStaff){
        //前端不知道怎样获取value值，后续这里可以优化
        emergencyStaff.setUnitname(enterpriseService.queryById(emergencyStaff.getUnitid()).getEnterpriseName());
        String str = emergencyStaff.getSystemtype();
        String[] strarr = str.split(",");
        StringJoiner subStr = new StringJoiner(",");
        for (String s : strarr) {
            subStr.add(sysDirectoryService.queryByCode(s).getValue());
        }
        emergencyStaff.setSystemtypename(subStr.toString());
        iEmergencyStaffService.updateById(emergencyStaff);
        return CommonResult.success("修改成功") ;
    }
    @ApiOperation(value = "禁用报警维护人员信息", notes = "禁用报警维护人员信息")
    @GetMapping("/delete/{id}")
    public CommonResult delete (@PathVariable("id") Long id){
        EmergencyStaff emergencyStaff = new EmergencyStaff();
        emergencyStaff.setId(id);
        emergencyStaff.setIsdel(1);
        iEmergencyStaffService.updateById(emergencyStaff);
        return CommonResult.success("禁用成功");
    }

    @ApiOperation(value = "根据id获取报警维护人员信息", notes = "根据id获取报警维护人员信息")
    @GetMapping("/get/{id}")
    public CommonResult<EmergencyStaff> get (@PathVariable("id") Long id){
        EmergencyStaff  emergencyStaff= iEmergencyStaffService.getById(id);
        return CommonResult.success(emergencyStaff);
    }
    @ApiOperation(value = "启用或禁用报警维护人员", notes = "启用或禁用报警维护人员")
    @PostMapping("/updateisdel")
    public CommonResult updateIsDel(@RequestBody EmergencyStaff emergencyStaff){
        iEmergencyStaffService.updateById(emergencyStaff);
        return CommonResult.success("更新成功");
    }
}
